<template>
  <div class="dialog">
    <el-dialog
      :visible.sync="dialogVisible"
      width="550px"
      append-to-body
      :modal="true"
      @close="closeDialog"
    >
      <span slot="title" class="dialog-title">{{ dialogTitle }}</span>
      <div class="dialogBody">
        <el-form ref="mealForm" :label-position="labelPosition" :model="mealForm" size="mini">
          <el-form-item label="规则名称" style="width:350px" prop="ruleName">
            <el-input v-model="mealForm.ruleName" placeholder="" :readonly="isEdit"></el-input>
          </el-form-item>
          <el-form-item label="选择套餐" prop="packageIds">
            <div class="infinite-list-wrapper" style="overflow:auto">
              <el-checkbox-group v-model="mealForm.packageIds" v-infinite-scroll="load">
                <el-checkbox v-for="item of dataCheckbox" :key="item.id" :label="item.id">{{ item.packageName }}</el-checkbox>
              </el-checkbox-group>
            </div>
          </el-form-item>
          <el-form-item label="套餐规则" prop="rules">
            <el-checkbox-group v-model="mealForm.rules">
              <el-checkbox v-for="item of dict.package_rule" :key="item.value" :label="item.value">{{ item.label }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item class="btn">
            <el-button type="primary" @click="submitForm('mealForm')">保存</el-button>
            <el-button @click="resetForm('mealForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addPackageRule, queryRulePackage, editPackageRule, query } from '@/api/packageRule';
import { mapGetters } from 'vuex';
export default {
  components: {},
  dicts: ['order_treat_stage', 'package_rule'],
  data() {
    return {
      dialogVisible: false,
      dialogTitle: '新增规则',
      isEdit: false, // 是否编辑
      labelPosition: 'top',
      dataCheckbox: [], // 套餐数据
      pageIndex: 0,
      total: 0,
      mealForm: {
        createBy: null,
        ruleName: null, // 规则名称
        packageIds: [], // 选择套餐
        rules: [] // 套餐规则
      }
    };
  },
  computed: {
    ...mapGetters(['user'])
  },
  methods: {
    load() {
      if (this.dataCheckbox.length && this.total >= this.dataCheckbox.length) {
        return;
      }
      this.queryLogList({ pageIndex: this.pageIndex, pageSize: 50 });
      this.pageIndex += 1;
    },
    queryLogList(params) {
      query(params).then(res => {
        this.dataCheckbox = res.content || [];
        this.total = parseInt(res.totalElements) || 0;
      }).catch(er => {});
    },
    isShow(item = {}, isEdit = false) {
      this.dialogVisible = true;
      this.isEdit = isEdit;
      if (isEdit) {
        this.dialogTitle = '编辑规则';
        const _meal = this.mealForm;
        queryRulePackage({ id: item.id }).then(res => {
          for (const key in _meal) {
            if (key !== 'packageIds' && key !== 'rules') {
              _meal[key] = res[key] || '';
            }
            _meal.enabled = res.enabled;
            _meal.id = res.id;
            _meal.packageIds = JSON.parse(res.packageIds);
            _meal.rules = JSON.parse(res.rules);
          }
        }).catch(err => { console.log(err); });
      }
    },
    // 关闭弹窗
    closeDialog() {
      this.resetForm();
    },
    // 取消
    resetForm(mealForm) {
      this.$refs.mealForm.resetFields();
      this.dialogVisible = false;
    },
    // 提交
    submitForm(mealForm) {
      this.$refs[mealForm].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            editPackageRule(this.mealForm).then(res => {
              this.$notify.success('编辑成功');
              this.resetForm();
              this.$emit('addPackageRuleSuccess');
            }).catch(err => { console.log(err); });
          } else {
            this.mealForm.createBy = this.user.id;
            addPackageRule(this.mealForm).then(res => {
              this.$notify.success('新增成功');
              this.resetForm();
              this.$emit('addPackageRuleSuccess');
            }).catch(err => { console.log(err); });
          }
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
>>> .el-dialog{
  margin-top: 5vh !important;
}
.infinite-list-wrapper{
  width: 100%;
  max-height: 150px;
}
.btn{
  >>> .el-form-item__content{
    margin: 0 auto !important;
    text-align: center;
  }
}
</style>
